<html>

<head>
    <title>grapecity_spreadJS_demo</title>
    <script src="jquery_3.1.1.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="gc.spread.sheets.excel2013white.14.2.2.css" type="text/css">
    <script src="gc.spread.sheets.all.14.2.2.min.js" type="text/javascript"></script>
    <script src="gc.spread.excelio.14.2.2.min.js" type="text/javascript"></script>
    <script src="https://cdn.grapecity.com.cn/spreadjs/scripts/FileSaver.min.js" type="text/javascript"></script>
    <script src="gc.spread.sheets.resources.zh.14.2.2.min.js" type="text/javascript"></script>
    <script src="gc.spread.sheets.print.14.2.2.min.js" type="text/javascript"></script>
    <script src="gc.spread.sheets.pdf.14.2.2.min.js" type="text/javascript"></script>
    <script src="gc.spread.sheets.charts.14.2.2.min.js" type="text/javascript"></script>
    <script src="v14.js" type="text/javascript"></script>
    <style type="text/css">
        body {
            background: rgb(250, 250, 250);
            color: #333;
            font-family: mtcorsva !important;
        }
        
        #ss {
            border: 1px #ccc solid;
        }
        
        .container {
            width: 95%;
            background: rgb(250, 250, 250);
            margin: 0 auto;
            height: 400px;
        }
        
        .full-height {
            height: 100%;
        }
        
        .left {
            height: 100%;
            overflow: auto;
        }
    </style>
</head>

<body>
    <div class="container">

        <div class="full-height clearfix mt2">

            <div class="inputContainer">
                <p>
                    规则： （1）被修约的数字小于5时，该数字舍去； （2）被修约的数字大于5时，则进位； （3）被修约的数字等于5时，要看5前面的数字，若是奇数则进位，若是偶数则将5舍掉，即修约后末尾数字都成为偶数；若5的后面还有不为“0”的任何数，则此时无论5的前面是奇数还是偶数，均应进位。
                </p>
            </div>



            <div class="col col-11 full-height ">
                <div id="ss" style="height:400px; font-family:simkai"></div>

            </div>
        </div>
    </div>
    <script type="text/javascript">
        // Title:自定义函数：四舍六入
         // Description：自定义函数：四舍六入
         // Tag:自定义函数，四舍六入
        
        
         var FdaFunction = function() {
             this.name = "FDA";
             this.minArgs = 1;
             this.maxArgs = 2;
         };
         FdaFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
         FdaFunction.prototype.description = function() {
             return {
                 description: "对value进行四舍六入五留双修约，保留小数点后指定位数",
                 parameters: [{
                     name: "value",
                     repeatable: false,
                     optional: false
                 }, {
                     name: "places",
                     repeatable: false,
                     optional: false
                 }]
             }
         }
         FdaFunction.prototype.isContextSensitive = function() {
             return true;
         }
         FdaFunction.prototype.evaluate = function(context, value, places) {
             var context = arguments[0];
             var num;
             if (!isNaN(parseInt(value)) && !isNaN(parseInt(places))) {
        
                 var viewport = designer.wrapper.spread.getActiveSheet().getFormatter(context.row, context.column, GC.Spread.Sheets.SheetArea.viewport);
                 if (isEmpty(viewport)) {
                     designer.wrapper.spread.getActiveSheet().setFormatter(context.row, context.column, "@", GC.Spread.Sheets.SheetArea.viewport);
                 }
                 value = NP.strip(value, value.toString().length);
                 /*value = NP.strip(value);*/
                 if (value < 0) {
                     num = -value;
                 } else {
                     num = value;
                 }
        
                 if (places < 0)
                     return value;
                 var v = Number(num);
                 var s = v + "";
                 var result = "";
                 if (s.indexOf(".") != -1) {
                     var s_1 = s.substring(s.indexOf(".") + 1);
                     if (s_1.length == parseInt(places) + 1) {
                         if (s_1.endsWith("5")) {
                             var s_2 = s.substr(0, s.length - 1);
                             var n = Number(s_2);
                             var s_n = n.toFixedELN(places);
                             var x = s_n.substring(s_n.length - 1);
                             if (Number(x) % 2 == 0) {
                                 result = s_n;
                             }
                         }
                     }
                 }
                 if (result == "") {
                     result = v.toFixedELN(places);
                 }
        
                 if (value < 0) {
                     result = "-" + result;
                     if (result.startsWith("-0")) {
                         if (Number(result) == 0)
                             result = result.substring(1, result.length);
                     }
                 }
        
                 return result;
             } else {
                 return "#VALUE!";
             }
         }
         var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {
             sheetCount: 2
         });
         var sheet = spread.getActiveSheet();
         sheet.getCell(2, 1).text('自定义单元格：');
         sheet.getCell(2, 1).value(1.432);
         sheet.setColumnWidth(1, 120);
        
         var fda = new FdaFunction();
         sheet.addCustomFunction(fda);
        
         sheet.setFormula(2, 2, "=fda(C1)");
    </script>
</body>

</html>